﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Utils.Commonly
{
    public class CommonlyClass
    {
        /// <summary>
        /// 生成唯一ID
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static string GenerateId(string prefix = null)
        {
            if (prefix == null)
            {
                prefix = "";
            }
            else
            {
                prefix = prefix + "_";
            }
            return prefix + new Random().Next(1000, 9999).ToString() + TimeHelper.GetTimeStamp();
        }

        /// <summary>
        /// 获取accessid
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            string cl = CommonlyClass.GenerateId();
            MD5 md5 = MD5.Create(); //实例化一个md5对像
                                    // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            return Convert.ToBase64String(s);
        }
    }
}
